﻿using QxFramework.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TargetUI : UIBase
{
    private int PlayerID = 0;
    public override void OnDisplay(object args)
    {
        Cursor.visible = false;
        base.OnDisplay(args);
        PlayerID = int.Parse(args.ToString());
        transform.Find("Text").GetComponent<Text>().text = PlayerID.ToString() + "P";
    }

    void Update()
    {
        GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/UI/Aim");

        if (!GameMgr.Get<IMonoManager>().GetAllMono()._MonoList.ContainsValue(GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono))
        {
            UIManager.Instance.Close(this);
            return;
        }
        if (GameMgr.Get<IMonoManager>().GetObj(GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono) == null)
        {
            return;
        }

        if (PlayerID == 0)
        {
            Vector3 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
            Vector3 mousePositionOnScreen = Input.mousePosition;
            gameObject.transform.position = mousePositionOnScreen;
        }
        if (PlayerID == 1)
        {
            if (Input.GetAxis("AttackX") != 0 || Input.GetAxis("AttackY") != 0)
            {
                Vector3 pos = new Vector3(325 * Input.GetAxis("AttackX"), 325 * Input.GetAxis("AttackY"), 0);
                Vector3 player = Camera.main.WorldToScreenPoint(GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[PlayerID].PlayerMono.Position);
                gameObject.transform.position = new Vector3(pos.x + player.x, pos.y + player.y, pos.z + player.z);
            }
        }

        transform.Find("TextThing").GetComponent<Text>().text = (GameMgr.Get<IBattleManager>().GetBattleData().InBattle) ? GetTargetName(InputManager.Instance.GetOverGameObject()): "";

        Cursor.visible = InputManager.Instance.GetOverUI();
        GetComponent<Image>().enabled = !InputManager.Instance.GetOverUI();
        transform.Find("Text").GetComponent<Text>().enabled = !InputManager.Instance.GetOverUI();
        transform.Find("TextThing").GetComponent<Text>().enabled = !InputManager.Instance.GetOverUI();
    }

    private string GetTargetName(GameObject obj)
    {
        if (obj == null)
        {
            return "";
        }
        else if (obj.name.Contains("Enemy"))
        {
            GetComponent<Image>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/UI/Aim2");
            if (GameMgr.Get<IMonoManager>().GetMono(obj.name).BuffList.ContainsKey(1))
            {
                return "敌人（燃烧）";
            }
            else if (GameMgr.Get<IMonoManager>().GetMono(obj.name).BuffList.ContainsKey(2))
            {
                return "敌人（寒冷）";
            }
            else if (GameMgr.Get<IMonoManager>().GetMono(obj.name).BuffList.ContainsKey(5))
            {
                return "敌人（致盲）";
            }
            else if (GameMgr.Get<IMonoManager>().GetMono(obj.name).BuffList.ContainsKey(6))
            {
                return "敌人（麻痹）";
            }
            else if (GameMgr.Get<IMonoManager>().GetMono(obj.name).BuffList.ContainsKey(7))
            {
                return "敌人（缓慢）";
            }
            else if (GameMgr.Get<IMonoManager>().GetMono(obj.name).BuffList.ContainsKey(8))
            {
                return "敌人（冰冻）";
            }
            else
            {
                return "敌人";
            }
        }
        else if (obj.name.Contains("Flame"))
        {
            if (obj.GetComponent<SceneMonoBase>() == null)
            {
                return "";
            }
            if (obj.GetComponent<SceneMonoBase>().sceneMonoData == null)
            {
                return "";
            }
            switch (obj.GetComponent<SceneMonoBase>().sceneMonoData.attr)
            {
                case MonoAttribute.Fire: return "火焰";
                case MonoAttribute.Ice: return "冰雾";
                case MonoAttribute.Steam: return "水雾";
                case MonoAttribute.Electric: return "电云";
                case MonoAttribute.Oil: return "油雾";
                default: return "";
            }
        }
        else if (obj.name.Contains("Ground"))
        {
            switch (obj.GetComponent<SceneMonoBase>().sceneMonoData.attr)
            {
                case MonoAttribute.Fire: return "火法阵";
                case MonoAttribute.Ice: return "冰法阵";
                case MonoAttribute.Shine: return "光法阵";
                case MonoAttribute.Dark: return "暗法阵";
                case MonoAttribute.Steam: return "水法阵";
                case MonoAttribute.Electric: return "电法阵";
                case MonoAttribute.Oil: return "油法阵";
                default: return "地板";
            }
        }
        else
        {
            return "";
        }
    }

}
